package spittr.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

@Component
public class SpittleRepositoryImpl implements SpittleRepository {
	private List<Spittle> list;

	public SpittleRepositoryImpl() {
		list = new ArrayList<>();

		for (int i = 0; i < 100; i++) {
			list.add(new Spittle("spittle" + i, new Date()));
		}

		System.out.println("findSpittles method...");
	}

	@Override
	public List<Spittle> findSpittles(long max, int count) {

		int from;
		
		System.out.println("list.size=" + list.size());
		for ( from = 0; from < list.size(); from++)
		{
//			System.out.println("getid=" + list.get(from).getId() + ", max=" + max);
			if (list.get(from).getId() == max)
				break;
		}
	
		System.out.println("from=" + from);
		if (from >= list.size()){
			//return null;
			throw new SpittleNotFoundException();
		}
		
		return list.subList(from, (from+count) > list.size() ? list.size() : (from+count));
	}

	@Override
	public void addSpittles(Spittle spittles) {
		list.add(spittles);
	}

	@Override
	public Spittle getSpittle(Long Id) {
		
		for (Spittle spittle: list){
			if (spittle.getId() == Id)
				return spittle;
		}
		return null;

	}
}
